//
//  CHWXQYHeaderView.m
//  CarFile
//
//  Created by 陈浩 on 2017/4/12.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHWXQYHeaderView.h"
@interface CHWXQYHeaderView()
@property (nonatomic ,strong) UIImageView *iconImgView;
@property (nonatomic ,strong) UILabel *titleLabel;
@property (nonatomic ,strong) UIImageView *arrowImgView;
@end
@implementation CHWXQYHeaderView



-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.iconImgView = [[UIImageView alloc] initWithFrame:CGRectMake(13, (self.h - 20) /2, 20, 20)];
        [self addSubview:self.iconImgView];
      
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.iconImgView.frame) +5, 0, UIScreenW/2, self.h)];
        self.titleLabel.font = Font(14);
        self.titleLabel.textColor = [UIColor colorWithHexString:@"148EC7" alpha:1.0f];
        [self addSubview:self.titleLabel];
        
       // UIImageView *arrowImgView = [[UIImageView alloc] initWithFrame:CGRectMake(UIScreenW - 15 -10, (self.h - 15) /2, 10, 15)];
      //  arrowImgView.image = [UIImage imageNamed:@"right_arrow"];
     //   [self addSubview:arrowImgView];
      //  self.arrowImgView = arrowImgView;
        
        
    //    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
      //  [self addGestureRecognizer:tap];
        
    }
    return self;
    
}


-(void)tapAction{
    
    self.model.isOpened = !self.model.isOpened;
    
    if (self.block) {
        self.block();
    }
    
}

-(void)willMoveToSuperview:(UIView *)newSuperview{
    
    // 设置箭头状态
    if (self.model.isOpened) {
        self.arrowImgView.transform = CGAffineTransformMakeRotation(M_PI_2);
    } else {
        self.arrowImgView.transform = CGAffineTransformIdentity;
    }
    
}

/**
 *  当一个控件被添加到父控件上以后调用的方法
 *  我们需要在 新的headerView被添加到tabelView以后，再旋转当前headerView的箭头
 */
- (void)didMoveToSuperview
{
    // 3. 让箭头旋转
    if (self.model.isOpened) { // 展开
        self.arrowImgView.transform = CGAffineTransformMakeRotation(M_PI_2); // 90°
    }else{
        self.arrowImgView.transform = CGAffineTransformMakeRotation(0); // 还原位置
    }
}



-(void)setModel:(WX_Model *)model{
    _model = model;
    self.titleLabel.text = model.title;
    self.iconImgView.image = [UIImage imageNamed:model.iconName];
    
    
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-1, self.frame.size.width, 1)];
    line.backgroundColor = [UIColor colorWithHexString:@"999999" alpha:0.3];
    
    [self addSubview:line];
    
}
@end
